<?php

class Admin_Model_GalleryItem {
	
	const PATH = '/pics/gallery/';
	const TEMP = 'tmp/';
	
	public static function getPath($addDot = true){return $addDot?'.'.self::PATH:self::PATH;}
	public static function getTmpPath($addDot = true){return ($addDot?'.'.self::PATH:self::PATH).self::TEMP;}
	
	public $Id;
	public $SizeId;
	public $Ext;
	public $Alt;
	public $Title;
	public $Width;
	public $Height;
	public $Order;
	
	
	function __toString(){
    	return "{$this->Id}"; 
    }
	
	public function __construct($options = null)
    {
        if (is_array($options)) {
            $this->setOptions($options);
        }
    }
	public function setOptions(array $options)
    {
        $methods = get_class_methods($this);
        foreach ($options as $key => $value) {
            $method = 'set' . ucfirst($key);
            if (in_array($method, $methods)) {
                $this->$method($value);
            }
        }
        return $this;
    }
    public function toArray($onlyFields=false)
    {
    	$vars = get_class_vars(get_class($this));
    	if (is_array($onlyFields))
    		$vars = array_intersect_key($vars,$onlyFields);
        foreach ($vars as $var=>$value)
        {
        	$vars[$var] = $this->$var;
        }
        return $vars;
    }
	public function __set($name, $value)
    {
        $method = 'set' . $name;
        if (('mapper' == $name) || !method_exists($this, $method)) {
            throw new Exception('Invalid property');
        }
        $this->$method($value);
    }

    public function __get($name)
    {
        $method = 'get' . $name;
        if (('mapper' == $name) || !method_exists($this, $method)) {
            throw new Exception('Invalid property');
        }
        return $this->$method();
    }
	
	public function getId(){
		return $this->Id;	
	}
	
	public function getSizeId(){
		return $this->SizeId;
	}
	
	public function getExt(){
		return $this->Ext;	
	}
	
	public function getAlt(){
		return $this->Alt;	
	}
	
	public function getTitle(){
		return $this->Title;	
	}
	
	public function getWidth(){
		return $this->Width;	
	}
	
	public function getHeight(){
		return $this->Height;	
	}
	
	public function getOrder(){
		return $this->Order;	
	}
	public function setId($value){
		$this->Id = $value;
		return $this;	
	}
	
	public function setSizeId($value){
		$this->SizeId = $value;
		return $this;
	}
	
	public function setExt($value){
		$this->Ext = $value;
		return $this;	
	}
	
	public function setAlt($value){
		$this->Alt = $value;
		return $this;	
	}
	
	public function setTitle($value){
		$this->Title = $value;
		return $this;	
	}
	
	public function setWidth($value){
		$this->Width = $value;
		return $this;	
	}
	
	public function setHeight($value){
		$this->Height = $value;
		return $this;	
	}
	
	public function setOrder($value){
		$this->Order = $value;
		return $this;	
	}
    
    public function getFileName(){
        return $this->getId().'_'.$this->getSizeId().'_'.$this->getWidth().'x'.$this->getHeight().'.'.$this->getExt();
    }
    
    public function getHTML($cssClass=null,$width=null, $height=null, $alt=null, $title=null){
        return '<img'.(null!=$cssClass?' class="'.$cssClass.'"':'' )
            .' src="/pics/gallery/'.$this->getFileName().'"'
            .' width="'.(null!=$width?$width:$this->getWidth()).'"'
            .' height="'.(null!=$height?$height:$this->getHeight()).'"'
            .' alt="'.(null!=$alt?$alt:$this->getAlt()).'"'
            .' title="'.(null!=$title?$title:$this->getTitle()).'"'
            .'/>';
    }
}
?>